package com.gxa.advice;

import com.gxa.dto.ResultDto;
import com.gxa.pojo.Log;
import com.gxa.pojo.User;
import com.gxa.service.LogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Time;
import java.sql.Timestamp;

@Component
@Aspect
public class LoginServiceProxy {

    @Autowired
    private LogService logService;

    @AfterReturning(pointcut = "execution(* com.gxa.controller.LoginController.loginByName(..))", returning = "res")
    public void afterLoginByNameReturning(JoinPoint joinPoint, Object res){
        Log log = new Log();
        Object[] joinPointArgs = joinPoint.getArgs();
        for (Object joinPointArg : joinPointArgs) {
            if (joinPointArg instanceof User){
                log.setLogUserName(((User) joinPointArg).getUserName());
            }
        }
        ResultDto res1 = (ResultDto) res;
        if (res1.getCode()==200){
            log.setLogIsSuccess((byte) 1);
        }else {
            log.setLogIsSuccess((byte) 0);
        }
        log.setLogContent("使用账户密码登录 ："+res1.getMsg());
        log.setLogTime(new Timestamp(System.currentTimeMillis()));
        logService.add(log);
    }

    @AfterReturning(pointcut = "execution(* com.gxa.controller.LoginController.loginByPhone(..))", returning = "res")
    public void afterLoginByPhoneReturning(JoinPoint joinPoint, Object res){
        Log log = new Log();
        Object[] joinPointArgs = joinPoint.getArgs();
        for (Object joinPointArg : joinPointArgs) {
            if (joinPointArg instanceof User){
                log.setLogUserName(((User) joinPointArg).getUserPhone().toString());
            }
        }
        ResultDto res1 = (ResultDto) res;
        if (res1.getCode()==200){
            log.setLogIsSuccess((byte) 1);
        }else {
            log.setLogIsSuccess((byte) 0);
        }
        log.setLogContent("使用手机号登录 ："+res1.getMsg());
        log.setLogTime(new Timestamp(System.currentTimeMillis()));
        logService.add(log);
    }
}
